<?php
class groupController extends base
{

    public function __construct($type='api')
    {
        parent::__construct($type);
        if($type == 'api' && (common::request('a' != 'searchGroup') || common::request('a' != 'recommendGroupList')))
        {
            $uid = $this->uid;
            if(empty($uid))
            {
                $_ENV['error']->getErrors(201);
                exit;
            }
        }
    }
    /**
     * 群组设置
     */
    public function dosettings()
    {
        $uid = $this->uid;
        empty($uid) && $uid = 0;

        $maxCreateNum = $this->load('group')->getMaxGroupNum($uid); // 用户可创建的群组数
        $maxMemberNum = $this->config['group']['maxMember']; // 群组成员最大数量

        $this->result['result'] = compact('maxCreateNum', 'maxMemberNum');
    }
    /**
     * 搜索群组
     */
    public function dosearchGroup()
    {
        $gid = common::request('gid', 'G', null, true);
        $keyword = trim($gid);
        if(empty($gid))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $limit = $this->vn >= 2400 ? 20 : 1;
        $this->result['result'] = $this->load('group')->searchGroup($keyword, $limit);
    }

    /**
     * 创建群组
     */
    public function docreateGroup()
    {
        $name = common::request('name', 'P');
        $slogan = common::request('slogan', 'P');
        $province = common::request('province', 'P');
        $city = common::request('city', 'P');
        $invitee = common::request('invitee', 'P');
        $verify = common::request('verify','P',0);
        $availeGroupNum = $this->load('group')->getVaileGroupNum($this->uid);
        if($availeGroupNum <= 0)
        {
            $_ENV['error']->getErrors(904);
            exit;
        }
        if(empty($name) || !common::is_json($invitee))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        if(mb_strlen($name,'utf8') > 15)
        {
            $_ENV['error']->getErrors(900);
            exit;
        }
        if(mb_strlen($slogan,'utf8') > 30)
        {
            $_ENV['error']->getErrors(901);
            exit;
        }
        if($this->load('badwords')->check($slogan) || $this->load('badwords')->check($name))
        {
            $_ENV['error']->getErrors(605);
            exit;
        }
        $invitee = json_decode($invitee, true);
        $this->result['result'] = $this->load('group')->createGroup($this->uid, $name, $slogan, $province, $city, $invitee,$verify);
    }
    /**
     * 邀请用户加入群组
     */
    public function doinviteMem()
    {
        $gid = common::request('gid', 'G');
        $invitee = common::request('invitee', 'G');
        if($gid < 1 || !common::is_json($invitee))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $groupInfo = $this->load('group')->getGroupInfo($gid);
        if(empty($groupInfo) || $groupInfo['isDismiss'] == 1)
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $role = $this->load('group')->getGroupRole($gid, $this->uid);

        if($this->vn < 2400){
            if($role != 1 && $role != 2 ){//群主、管理员才可以操作
                $_ENV['error']->getErrors(903);
                exit;
            }
        }else{
            if($role != 1 && $role != 2 && $role != 0){//群主、管理员、普通成员才可以操作
                $_ENV['error']->getErrors(903);
                exit;
            }
        }

        $invitee = json_decode($invitee, true);
        $is_success = $this->load('group')->groupInvite($this->uid, $gid, $invitee) != false ? 1:0;
        $this->result['result']['is_success'] = $is_success;
        $this->breakCall();
    }
    /**
     * 处理群组邀请
     */
    public function doagreeInvitation()
    {
        $gid = common::request('gid', 'G');
        $isAgree = common::request('isAgree', 'G', 0);

        $this->result['result']['status'] = $this->load('group')->doInvitation($gid, $this->uid, $isAgree);
    }
    /**
     * 群组资料
     */
    public function dogroupInfo()
    {
        $gid = common::request('gid', 'G');
        $groupInfo = $this->load('group')->getGroupInfo($gid);
        $result = array();
        $hUid = $this->redis->hget(REDIS_PREFIX.'GroupHost', $gid);
        $userInfo = $this->load('client')->getUserInfo($hUid);
        $hostInfo['role'] = 1;
        $hostInfo['uid'] = $hUid;
        $hostInfo['header'] = $userInfo['header'];
        $aUids = $this->load('group')->getGroupAdmin($gid);
        $admin[] = $hostInfo;
        foreach($aUids as $k=>$uid){
            $adminInfo = $this->load('client')->getUserInfo($uid);
            $item['role'] = 2;
            $item['uid'] = $uid;
            $item['header'] = $adminInfo['header'];
            $admin[] = $item;
        }
        $manage = $admin;
        $postList = array();
        $activity = array();
        $newsIds = $this->redis->zrevrange(REDIS_PREFIX.'GroupNews_'.$gid,0,0);
        foreach($newsIds as $key=>$news_id){
            $newInfo = $this->load('bbs')->getPostInfo($news_id);
            $postList[$key]['news_id'] = $news_id;
            if(isset($newInfo['title']) && !empty($newInfo['title'])){
                $postList[$key]['title'] = $newInfo['title'];
            }else{
                $postList[$key]['title'] = isset($newInfo['short_desc']) ? $newInfo['short_desc'] : $newInfo['content'];
            }
            $postList[$key]['thumb_img'] = array_slice($newInfo['thumb_img'],0,3);
        }
        $list = $this->load("groupActivity")->getActList($gid,0,'next',2);
        foreach($list as $k=>$aid){
            $info = $this->load('groupActivity')->getActByAid($aid);
            $activity[$k]['title'] = $info['title'];
            $activity[$k]['aid'] = $aid;
        }
        $auth = $this->db->getRow('select gid from r_group_auth where status != 1 and gid='.$gid);
        if(!empty($groupInfo))
        {
            $result['gid'] = $groupInfo['gid'];
            $result['name'] = $groupInfo['name'];
            $result['slogan'] = $groupInfo['slogan'];
            $result['header'] = $groupInfo['header'];
            $result['sourceImg'] = $groupInfo['sourceImg'];
            $result['province'] = $groupInfo['province'];
            $result['city'] = $groupInfo['city'];
            $result['isSubmit'] = empty($auth) ? 0 : 1;
            $result['authenticate'] = $this->load('group')->isAuth($gid);
            $result['addtime'] = $groupInfo['addtime'];
            $result['isverify'] = isset($groupInfo['isverify']) ? intval($groupInfo['isverify']) : 0;
            $result['role'] = $this->load('group')->getGroupRole($gid, $this->uid);
            $result['status'] = $this->load('group')->getGroupStatus($this->uid, $gid);
            $result['members'] = $this->load('group')->getGroupRandomMem($gid, 6);
            $result['todayRun'] = $this->load('group')->getTodayRun($gid);
            $result['postNum'] = $this->redis->zsize(REDIS_PREFIX.'GroupNews_'.$gid);
            $result['actNum'] = $this->redis->zsize(REDIS_PREFIX."GroupActivity_".$gid);
            $result['manage'] = $manage;
            $result['joinNum'] = $groupInfo['members'];
            $result['postList'] = $postList;
            $result['activity'] = $activity;
            $this->result['result'] = $result;
        }else{
            $_ENV['error']->getErrors(905);
            exit;
        }

    }
    /**
     * 修改群组资料
     */
    public function dosetGroupInfo()
    {
        $gid = common::request('gid', 'P');
        $name = common::request('name', 'P');
        $slogan = common::request('slogan', 'P');
        $province = common::request('province', 'P');
        $city = common::request('city', 'P');
        $isverify = common::request('verify',"P");
        $role = $this->load('group')->getGroupRole($gid, $this->uid);
        if($role != 1 && $role != 2)
        {
            $_ENV['error']->getErrors(903);
            exit;
        }
        if(empty($name))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        if(mb_strlen($name,'utf8') > 15)
        {
            $_ENV['error']->getErrors(900);
            exit;
        }
        if(mb_strlen($slogan,'utf8') > 30)
        {
            $_ENV['error']->getErrors(901);
            exit;
        }
        $groupInfo = compact('gid','name','slogan','province','city','isverify');
        $this->load('group')->setGroupInfo($gid, $groupInfo);
        $info = $this->load('group')->getGroupInfo($gid);
        if(!empty($info))
        {
            $result = array();
            $result['gid'] = $info['gid'];
            $result['name'] = $info['name'];
            $result['slogan'] = $info['slogan'];
            $result['header'] = $info['header'];
            $result['sourceImg'] = $info['sourceImg'];
            $result['province'] = $info['province'];
            $result['city'] = $info['city'];
            $result['isverify'] = intval($info['isverify']);
            $this->result['result'] = $result;
        }
        else
        {
            $_ENV['error']->getErrors(100);
            exit;
        }
    }
    /**
     * 群成员列表
     */
    public function dogroupMembers()
    {
        $gid = intval(common::request('gid', 'G'));
        $offset = common::request('offset','G',20);
        /*
        $role = $this->load('group')->getGroupRole($gid, $this->uid);
        if($role < 0 || $role > 3)
        {
            $_ENV['error']->getErrors(903);
            exit;
        }
        */
        $host = $this->redis->hget(REDIS_PREFIX.'GroupHost', $gid);
        $admin = $this->load('group')->getGroupAdmin($gid);
        $civilian = $this->load('group')->getCililian($gid,0,'next',$offset);

        $result = compact('host','admin','civilian');
        foreach ($result as & $v) {
            if(is_array($v))
            {
                $tmp = array();
                foreach ($v as $key=>$uid) {
                    $userInfo = $_ENV['client']->getUserInfo($uid);
                    if(!empty($userInfo))
                    {
                        $tmp[] = array('uid' => $userInfo['uid'],
                            'header' => $userInfo['header'],
                            'nickname' => $userInfo['nickname'],
                            'gender' => $userInfo['gender'],
                            'level' => $userInfo['level'],
                            'total_mileage' => $userInfo['total_mileage'],
                            'total_rate' => $userInfo['total_rate'],
                        );
                    }
                    unset($userInfo);
                }
                $v = array_values($tmp);
            }
            else
            {
                $userInfo = $_ENV['client']->getUserInfo($v);
                $v = array();
                if(!empty($userInfo))
                {
                    $v['uid'] = $userInfo['uid'];
                    $v['header'] = $userInfo['header'];
                    $v['nickname'] = $userInfo['nickname'];
                    $v['gender'] = $userInfo['gender'];
                    $v['level'] = $userInfo['level'];
                    $v['total_mileage'] = $userInfo['total_mileage'];
                    $v['total_rate'] = $userInfo['total_rate'];
                }
                unset($userInfo);
            }
        }

        $groupInfo = $this->load('group')->getGroupInfo($gid);
        $maxMember = !empty($groupInfo['member_limit']) ? $groupInfo['member_limit'] : $this->config['group']['maxMember'];

        $result['maxAdmin'] = $this->config['group']['adminLimit'];
        $result['maxCivilian'] = $maxMember - count($admin) -1;
        !$result['host'] && $result['host'] = '';
        $this->result['result'] = $result;
    }
    /**
     * 群组普通成员
     */
    public function dogroupCivilians()
    {
        $gid = common::request('gid', 'G');
        $uid = common::request('giuidd', 'G');
        $direc = common::request('direc', 'G', 'next');
        $offset = common::request('offset', 'G', 20);

        // 验证用户身份
        /*
        $role = $this->load('group')->getGroupRole($gid, $this->uid);
        if($role < 0 || $role > 3)
        {
            $_ENV['error']->getErrors(903);
            exit;
        }
        */
        // 获取成员列表
        $civilianUids = $this->load('group')->getCililian($gid, $uid, $direc, $offset);

        $cilivilan = array(); // 群组普通成员列表
        foreach ($civilianUids as $memberId)
        {
            $userInfo = $_ENV['client']->getUserInfo($memberId);
            if(!empty($userInfo))
            {
                $cilivilan[] = array('uid' => $userInfo['uid'],
                    'header' => $userInfo['header'],
                    'nickname' => $userInfo['nickname'],
                    'gender' => $userInfo['gender'],
                    'level' => $userInfo['level'],
                    'total_mileage' => $userInfo['total_mileage'],
                    'total_rate' => $userInfo['total_rate'],
                );
            }
        }
        $this->result['result'] = $cilivilan;
    }

    /**
     * 设置管理员
     */
    public function dosetAdmin()
    {
        $gid = common::request('gid', 'G');
        $uids = common::request('uids', 'G');

        if(empty($gid) || !common::is_json($uids) || $uids == '[]' || $uids == '[""]')
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $gInfo = $this->load('group')->getGroupInfo($gid);
        if(empty($gInfo) || (isset($gInfo['isDismiss']) && $gInfo['isDismiss'] == 1))
        {
            $_ENV['error']->getErrors(905);
            exit;
        }
        if($this->uid != $this->redis->hget(REDIS_PREFIX.'GroupHost', $gid))
        {
            $_ENV['error']->getErrors(903);
            exit;
        }
        $uids = json_decode($uids, true);
        $uids = array_unique($uids);
        $is_success = $this->load('group')->setGroupAdmin($gid, $uids);
        $this->result['result']['is_success'] = $is_success;
        $this->breakCall();
        // if($is_success == 1)
        // {
        //     $data = array('title'   => sprintf("你被群主 添加为 %s 管理员", $gInfo['name']),
        //                   'text'    => '',
        //                   'body'    => sprintf("你被群主 添加为 %s 管理员", $gInfo['name']),
        //                   'url'    => 'joggers://groupList'
        //                 );
        //     $this->load('push')->pushNoticeByUid($uids, $data);
        // }

    }
    /**
     * 取消管理员
     */
    public function docancelAdmin()
    {
        $gid = common::request('gid', 'G');
        $uid = common::request('uid', 'G');
        if(empty($gid) || empty($uid))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $gInfo = $this->load('group')->getGroupInfo($gid);
        if(empty($gInfo) || (isset($gInfo['isDismiss']) && $gInfo['isDismiss'] == 1))
        {
            $_ENV['error']->getErrors(905);
            exit;
        }
        if($this->uid != $this->redis->hget(REDIS_PREFIX.'GroupHost', $gid))
        {
            $_ENV['error']->getErrors(903);
            exit;
        }
        $result = $this->load('groupActivity')->isQuit($gid,$uid);
        if($result){
            if($this->vn>=2700){
                $_ENV['error']->getErrors(2005);
                exit;
            }else{
                $_ENV['error']->getErrors(914);
                exit;
            }
        }
        $this->vn >= 2700 && $this->load('groupActivity')->quitAct($uid,$gid);
        $is_success = $this->load('group')->cancelGroupAdmin($gid, $uid);
        $this->result['result']['is_success'] = $is_success;
        $this->breakCall();
        // if($is_success == 1)
        // {
        //     $data = array('title'   => sprintf("你被群主 取消了 %s 管理员", $gInfo['name']),
        //                   'text'    => '',
        //                   'body'    => sprintf("你被群主 取消了 %s 管理员", $gInfo['name']),
        //                   'url'    => 'joggers://groupList'
        //                 );
        //     $this->load('push')->pushNoticeByUid(array($uid), $data);
        // }
    }
    /**
     * 解散群组
     */
    public function dodismiss()
    {
        $gid = common::request('gid', 'G');
        if(empty($gid) )
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        /*
        $result = $this->load('groupActivity')->getIngAct($gid,0,1);
        $aids = $this->redis->zRangeByScore(REDIS_PREFIX."GroupActivity_".$gid,2,time());
        $flag = false;
        foreach($aids as $aid){
            if($this->redis->sIsMember(REDIS_PREFIX."ACTaccept_".$aid,$gid)){
                $stime = $this->redis->hget(REDIS_PREFIX."ActivityInfo_".$aid,'stime');
                $etime = $this->redis->hget(REDIS_PREFIX."ActivityInfo_".$aid,'etime');
                if(time() >= $stime && $etime >= time()){
                    $flag = true;
                    break;
                }
            }
        }
        if($result || $flag){
            if($this->vn >= 2700){
                $_ENV['error']->getErrors(2006);
                exit;
            }else{
                if($this->projectId == 1){
                    $this->result['result']['is_success'] = 1;
                    exit;
                }
                $_ENV['error']->getErrors(914);
                exit;
            }
        }
        */
        //$this->vn >= 2700 && $this->redis->del(REDIS_PREFIX."GroupActivity_".$gid);
        $this->result['result']['is_success'] = $this->load('group')->dismissGroup($gid, $this->uid);
    }
    /**
     * 申请加入群组
     */
    public function doapplyGroup()
    {
        $gid = common::request('gid', 'G');
        $reason = common::request('reason','G',"");


        $gInfo = $this->load('group')->getGroupInfo($gid);
        if(empty($gid) || ($this->vn >= 2700 && ( !isset($gInfo['isverify']) || isset($gInfo['isverify']) && intval($gInfo['isverify'])== 0 ) && empty($reason)))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        if(empty($gInfo) || (isset($gInfo['isDismiss']) && $gInfo['isDismiss'] == 1))
        {
            $_ENV['error']->getErrors(905);
            exit;
        }

        $maxMember = !empty($gInfo['member_limit']) ? $gInfo['member_limit'] : $this->config['group']['maxMember'];
        
        // 群组成员已满
        if($gInfo['members'] >= $maxMember)
        {
            $_ENV['error']->getErrors(910);
            exit;
        }

        $uid = $this->uid;
        $status = $this->load('group')->applyForGroup($gid, $uid,$reason);
        $this->result['result']['status'] = $status;
    }



    /**
     * 群组管理消息总数
     */
    public function doapplicationNum()
    {
        $gid = common::request('gid', 'G');
        if(empty($gid))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $this->result['result']['num'] = $this->load('group')->GApplyNum($gid, $this->uid);
    }
    /**
     * 群组管理消息列表
     */
    public function doapplicationList()
    {
        $gid = common::request('gid', 'G');
        $point = common::request('order', 'G');
        $direc = common::request('direc', 'G','next');
        $offset = common::request('offset', 'G', 20);
        if(empty($gid))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $this->result['result'] = $this->load('group')->GApplyList($gid, $this->uid,$point, $direc, $offset);
    }
    /**
     * 处理群组消息 申请同意
     */
    public function dodealGroupApply()
    {
        $gid = common::request('gid', 'G');
        $id = common::request('id', 'G');
        $dotype  = common::request('dotype', 'G',0);
        if(empty($gid) || empty($id))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $uid = $this->uid;
        $gInfo = $this->load('group')->getGroupInfo($gid);
        $maxMember = !empty($gInfo['member_limit']) ? $gInfo['member_limit'] : $this->config['group']['maxMember'];
        if($gInfo['members'] >= $maxMember)
        {
            $_ENV['error']->getErrors(910);
            exit;
        }
        if(empty($gInfo) || (isset($gInfo['isDismiss']) && $gInfo['isDismiss'] == 1))
        {
            $_ENV['error']->getErrors(905);
            exit;
        }
        $curStatus = $this->db->getOne("select status from r_group_add where gid = '{$gid}' and id = '{$id}' and (status < 4 or status = 10) and isShow = 1");
        if($curStatus === false){
            $_ENV['error']->getErrors(915);
            exit;
        }
        $role = $this->load('group')->getGroupRole($gid, $uid);
        if($role == 1 || $role == 2)//只是群主、群管理员的时候才可以操作
        {
            if($gInfo['members'] >= $maxMember)
            {
                $_ENV['error']->getErrors(910);
                exit;
            }
            $status = $this->load('group')->dealGroupApply($gid, $uid, $id, $dotype);
            $this->result['result']['status'] = $status;
            $this->breakCall();
            // if($status == 0 && $dotype == 1)
            // {
            //     $uid = $this->db->getOne("select uid from r_group_add whereid = '{$id}'");
            //     $data = array('title'   => sprintf("你申请加入 %s 已被验证通过", $gInfo['name']),
            //                   'text'    => '看看有哪些小伙伴吧！',
            //                   'body'    => sprintf("你申请加入 %s 已被验证通过", $gInfo['name']).',看看有哪些小伙伴吧！',
            //                   'url'    => 'joggers://groupList'
            //                 );
            //     $this->load('push')->pushNoticeByUid(array($uid), $data);
            // }
        }
        else
        {
            $_ENV['error']->getErrors(903);
            exit;
        }
    }
    /**
     * 群组邀请消息概况
     */
    public function donewInviteView()
    {
        $this->result['result'] = $this->load('group')->GInviteView($this->uid);
    }
    /**
     * 群组消息及群组列表
     */
    public function dogroupList()
    {
        $notice = $this->load('group')->getQuitNotice($this->uid);
        $invitation = $this->load('group')->invitationList($this->uid);
        $joined = $this->load('group')->myGroupList($this->uid);
        $this->result['result'] = compact('notice','invitation','joined');
    }
    /**
     * 群组列表(新)
     */
    public function domyGroupList()
    {
        $uid = $this->uid;
        $created = $this->load('group')->myGroupListByRole($uid, 1);
        $joined = $this->load('group')->myGroupListByRole($uid, 3);
        $verify = $this->load('group')->getVerifyByUid($uid);
        if($this->vn >= 2730){
            $list = array_merge($created,$joined,$verify);
            foreach($list as &$info){
                $info['newsNum'] = $this->redis->zsize(REDIS_PREFIX.'GroupNews_'.$info['gid']);
                $info['status'] = $this->load('group')->getGroupStatus($uid,$info['gid']);
            }
            $this->result['result']['list'] = $list;
        }else{
            $this->result['result'] = compact('created','joined','verify');
        }

    }
    /**
     * 推荐群组列表
     * @return
     */
    public function dorecommendGroupList(){
        $p = common::request('p', 'G', 1);
        $uid = $this->uid;

        $recommendGroup = array();
        $banner = $this->load('group')->getRecommendBanner();
        $gids = $this->load('group')->getRecommendList($p, $uid);
        if(!empty($gids))
        {
            $groupStatus = $this->load('group')->getGroupStatus($uid);
            foreach ($gids as $gid) {
                $groupInfo = $this->load('group')->getGroupInfo($gid);
                $recommendGroup[] = array('gid' => $gid,
                    'name' => $groupInfo['name'],
                    'header' => $groupInfo['header'],
                    'slogan' => $groupInfo['slogan'],
                    'role'  => $this->load('group')->getGroupRole($gid, $uid),
                    'status' => isset($groupStatus[$gid]) ? $groupStatus[$gid] : '7'
                );
            }
        }
        $this->result['result'] = compact('banner','recommendGroup');
    }
    
    /**
     * 删除成员
     */
    public function dodelMember()
    {
        $gid = common::request('gid', 'G');
        $uid = common::request('uid', 'G');

        $gInfo = $this->load('group')->getGroupInfo($gid);
        if(empty($gInfo) || (isset($gInfo['isDismiss']) && $gInfo['isDismiss'] == 1))
        {
            $_ENV['error']->getErrors(905);
            exit;
        }
        $result = $this->load('groupActivity')->isQuit($gid,$uid);
        if($result){
            if($this->vn >= 2700){
                //$_ENV['error']->getErrors(2006);
                //exit;
            }else{
                $_ENV['error']->getErrors(914);
                exit;
            }
        }
        $this->vn >= 2700 && $this->load('groupActivity')->quitAct($uid,$gid);
        $is_success = $this->load('group')->delMember($gid, $this->uid, $uid);
        $this->result['result']['is_success'] = $is_success;
        $this->breakCall();
        // if($is_success == 1)
        // {
        //     $data = array('title'   => sprintf("你已被群主移出 %s", $gInfo['name']),
        //                   'text'    => '',
        //                   'body'    => sprintf("你已被群主移出 %s", $gInfo['name']),
        //                   'url'    => 'joggers://groupList'
        //                 );
        //     $this->load('push')->pushNoticeByUid(array($uid), $data);
        // }
    }
    /**
     * 退出群组
     */
    public function doquit()
    {
        $gid = common::request('gid', 'G');
        if(empty($gid))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        if($this->vn >= 2700){
            $result = $this->load('groupActivity')->isQuit($gid,$this->uid);
            /*
            if($result){
                $_ENV['error']->getErrors(2006);
                exit;
            }
            */
            $this->load('groupActivity')->quitAct($this->uid,$gid,true);
        }
        $this->result['result']['is_success'] = $this->load('group')->quitGroup($gid, $this->uid);
    }
    /**
     * 设置目标
     */
    public function dosetTarget()
    {
        $gid = common::request('gid', 'G');
        $GTarget = common::request('GTarget', 'G',0);
        $UTarget = common::request('UTarget', 'G',0);
        $uid = $this->uid;
        $gInfo = $this->load('group')->getGroupInfo($gid);
        if(empty($gInfo) || (isset($gInfo['isDismiss']) && $gInfo['isDismiss'] == 1))
        {
            $_ENV['error']->getErrors(905);
            exit;
        }
        $role = $this->load('group')->getGroupRole($gid, $uid);
        if($role == 1 || $role == 2)
        {
            $this->load('group')->setGroupTarget($gid, $uid, $GTarget);
        }
        else if($role != 0)
        {
            $_ENV['error']->getErrors(903);
            exit;
        }
        $this->load('group')->setUserTarget($gid, $uid, $UTarget);
        $this->result['result'] = $this->load('group')->getTargetInfo($gid, date("Y-m"), $uid);
    }
    /**
     * 获取目标
     */
    public function doTarget()
    {
        $gid = common::request('gid', 'G');
        $month = common::request('month', 'G');
        $uid = $this->uid;
        $gInfo = $this->load('group')->getGroupInfo($gid);
        if(empty($gInfo) || (isset($gInfo['isDismiss']) && $gInfo['isDismiss'] == 1))
        {
            $_ENV['error']->getErrors(905);
            exit;
        }
        $role = $this->load('group')->getGroupRole($gid, $uid);
        if($role == -1 || $role == 3)
        {
            $_ENV['error']->getErrors(903);
            exit;
        }
        $this->result['result'] = $this->load('group')->getTargetInfo($gid, $month, $uid);
    }
    /**
     * 群组最佳记录
     */
    public function dobestRecord()
    {
        $gid = common::request('gid', 'G');
        $month = common::request('month', 'G');
        $uid = $this->uid;
        $gInfo = $this->load('group')->getGroupInfo($gid);
        if(empty($gInfo) || (isset($gInfo['isDismiss']) && $gInfo['isDismiss'] == 1))
        {
            $_ENV['error']->getErrors(905);
            exit;
        }
        /*
        $role = $this->load('group')->getGroupRole($gid, $uid);
        if($role == -1 || $role == 3)
        {
           $_ENV['error']->getErrors(903);
           exit;
        }
        */
        $total_mileage = $this->load('group')->getGroupRunTotal($gid);
        $records = $this->load('group')->getBestRecord($gid);
        $total_mileage=round($total_mileage,2);
        $this->result['result'] = compact('total_mileage', 'records');
    }
    /**
     * 群组跑步记录
     */
    public function dogroupRecord()
    {
        $gid = common::request('gid', 'G');
        $date = common::request('date', 'G');
        $needCalendar = common::request('needCalendar', 'G', 0);
        $gInfo = $this->load('group')->getGroupInfo($gid);
        if(empty($gInfo) || (isset($gInfo['isDismiss']) && $gInfo['isDismiss'] == 1))
        {
            $_ENV['error']->getErrors(905);
            exit;
        }
        $uid = $this->uid;
        $role = $this->load('group')->getGroupRole($gid, $uid);
        if($role == -1 || $role == 3)
        {
            $_ENV['error']->getErrors(903);
            exit;
        }
        empty($date) && $date = date("Y-m-d");
        $this->result['result']['groupName'] = $gInfo['name'];
        $this->result['result'] = $this->load('group')->getGroupRunRecord($gid, $date);
        $month = date("Y-m", strtotime($date));
        $needCalendar && $this->result['result']['runCalendar'] = $this->load('group')->getGroupRunCalendar($gid, $month);
    }
    /**
     * 里程战力榜
     */
    public function dogroupMRank()
    {
        $gid = common::request('gid', 'G');
        $type = common::request('type', 'G', 0);
        $start = common::request('start', 'G');
        if(empty($start))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $gInfo = $this->load('group')->getGroupInfo($gid);
        if(empty($gInfo) || (isset($gInfo['isDismiss']) && $gInfo['isDismiss'] == 1))
        {
            $_ENV['error']->getErrors(905);
            exit;
        }
        $uid = $this->uid;
        $role = $this->load('group')->getGroupRole($gid, $uid);
        if($role == -1 || $role == 3)
        {
            $_ENV['error']->getErrors(903);
            exit;
        }
        $start = strtotime($start);
        if($type == 0)
        {
            $key = date("Y-m", $start);
            $date = date("Y-m") == $key ? '本月' : $key;
            $mileage = $this->load('group')->getGroupRunMoWTotal($gid, $key);
        }
        else if($type == 1)
        {
            $key = common::getWeekth($start);
            $date = '本周';
            if($key != common::getWeekth())
            {
                $range = common::getWeekRange($start);
                extract($range);
                $date = date("Y-m-d", $monday) . ' ~ ' . date("Y-m-d", $sunday);
            }
            $mileage = $this->load('group')->getGroupRunMoWTotal($gid, $key);
        }
        else
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $data = $this->load('group')->getGMRank($gid, $key);
        $showReport = 1;
        $groupName = $gInfo['name'];
        $result = compact('type', 'groupName', 'date', 'mileage', 'data', 'showReport');
        $this->result['result'] = $result;
    }
    /**
     * 目标战力榜
     */
    public function dogroupTRank()
    {
        $gid = common::request('gid', 'G');
        $type = 0;
        $start = common::request('start', 'G');
        if(empty($start))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $gInfo = $this->load('group')->getGroupInfo($gid);
        if(empty($gInfo) || (isset($gInfo['isDismiss']) && $gInfo['isDismiss'] == 1))
        {
            $_ENV['error']->getErrors(905);
            exit;
        }
        $uid = $this->uid;
        $role = $this->load('group')->getGroupRole($gid, $uid);
        if($role == -1 || $role == 3)
        {
            $_ENV['error']->getErrors(903);
            exit;
        }
        $start = strtotime($start);
        $key = date("Y-m", $start);
        $date = date("Y-m") == $key ? '本月' : $key;
        $mileage = $this->load('group')->getGroupRunMoWTotal($gid, $key);
        $GTarget =  $this->load('group')->getGroupTarget($gid, $key);
        $targetRate = empty($GTarget) ? '0.00' : round($mileage/$GTarget, 3);
        $data = $this->load('group')->getGTRank($gid, $key);
        $showReport = 1;
        $groupName = $gInfo['name'];
        $result = compact('type', 'groupName', 'date', 'targetRate', 'data', 'showReport');
        $this->result['result'] = $result;
    }

    /**
     * 获取
     * @return [type] [description]
     */
    public function doIMUser(){
        $gid = common::request('gid', 'G', 0);
        $this->result['result'] = $this->load('group')->getImUser($gid);
    }

    /* ========================= 2.7.0 ========================= */
    /**
     * 群组首页
     */
    public function dohome(){
        $uid = $this->uid;
        $banner = $this->load('group')->getRecommendBanner();
        
        $res = $this->load('banner')->getGroupBanner2();
        $banner2 = [];
        $now = time();
        foreach($res as $row){
            if(@$row['startTime'] && strtotime($row['startTime']) > $now){
                continue;
            }
            if(@$row['endTime'] && strtotime($row['endTime']) < $now){
                continue;
            }
            $banner2[] = $row;
        }
        
        $gids = $this->load('group')->getRecommendList(1, 0, 100);
        $city = common::request('city','G','');
        foreach($gids as $k=>$gid){
            $status = $this->load('group')->getGroupStatus($uid,$gid);
            if($status == 0){
                unset($gids[$k]);
            }
        }
        $data = $this->load('group')->getGroupByUid($uid);
        $limit = 5;
        if($this->vn>=2730){
            $limit = 20;
        }
        $gids = array_slice(array_merge($gids,$data),0,$limit);
        $recommendGroup = array();
        foreach($gids as $key=>$gid){
            $recommendGroup[$key]['gid']= $gid;
            $sData = $this->load('group')->groupStatic($gid);
            if($this->projectId == 1){
                $members = "0";
            }else{
                $members = [];
            }
            if($this->vn >= 2730){
                $members = $sData['members'];
            }
            $groupInfo = $this->load("group")->getGroupInfo($gid);
            $recommendGroup[$key]['name'] = $groupInfo['name'];
            $recommendGroup[$key]['isverify'] = isset($groupInfo['isverify']) ? intval($groupInfo['isverify']) : 0;
            $recommendGroup[$key]['header'] = $groupInfo['header'];
            $recommendGroup[$key]['role'] = $this->load('group')->getGroupRole($gid,$uid);
            $act = $this->load('groupActivity')->getIngAct($gid,0,5);
            $recommendGroup[$key]['isActivity'] = empty($act) ? 0 : 1;
            $recommendGroup[$key]['totalMileage'] = floatval($this->load('group')->getTotalMileageByGid($gid));
            $recommendGroup[$key]['joinNum'] = $groupInfo['members'];
            $recommendGroup[$key]['slogan'] = $groupInfo['slogan'];
            $recommendGroup[$key]['status'] = $this->load('group')->getGroupStatus($uid,$gid);
            $recommendGroup[$key]['authenticate'] = $this->load('group')->isAuth($gid);
            $recommendGroup[$key]['members'] = $members;//群成员列表
            $recommendGroup[$key]['maleNum'] = $sData['maleNum'];//男性人数
            $recommendGroup[$key]['femaleNum'] = $groupInfo['members'] - $sData['maleNum'];//女性人数
            $recommendGroup[$key]['title'] = !isset($act[0]['title']) ? '' : $act[0]['title'];//活动名称
        }

        //获取推荐列表，并且过滤已参加的活动
        $recAct = $this->redis->zrevrange(REDIS_PREFIX."ACTRecommend",0,-1);
        $recommendActivity = [];
        foreach($recAct as $aid){
            $actInfo = $this->load('groupActivity')->getActByAid($aid);
            if(!$this->redis->sismember(REDIS_PREFIX."GAJoin_".$actInfo['gid']."_".$aid,$uid) && $actInfo['status'] == 0 && $actInfo['stime']>time()){
                $recommendActivity[] = $actInfo;
            }
        }
        $sNum = 5-count($recommendActivity);
        if($sNum > 0){
            $activity = array_merge($recommendActivity,$this->load('groupActivity')->nStartAct($sNum,$uid,$city));
        }else{
            $activity = array_slice($recommendActivity,0,5);
        }
        foreach($activity as &$info){
            $info['joinNum'] = $this->load('groupActivity')->joinNum($info['gid'],$info['aid']);
            $gInfo = $this->load('group')->getGroupInfo($info['gid']);
            $info['name'] = isset($gInfo['name']) ? $gInfo['name'] : '';
            $info['header'] = isset($gInfo['header']) ? $gInfo['header'] : '';
            $info['isNotice'] = intval($this->redis->sismember(REDIS_PREFIX."GAJoin_".$info['gid']."_".$info['aid'],$uid));
            $info['isClaim'] = intval($this->redis->sismember(REDIS_PREFIX."ACTaccept_".$info['aid'],$info['gid']));
            $info['isJoin'] = $this->load('groupActivity')->isJoin($info['aid'],$uid,$info['gid']);
            $info['role'] = $this->load('group')->getGroupRole($info['gid'], $uid);
            $info['curTime'] = time();//当前时间
            if(isset($info['type']) && $info['type'] == 1){
                $info['days'] = ceil(($info['etime']-$info['stime'])/24/3600);
            }
            unset($info['members']);
        }
        $this->result['result'] = compact('banner','banner2','recommendGroup','activity');
    }

    /**
     * 群组列表
     */
    public function dogList(){

        $uid = $this->uid;
        $type = common::request('type',"G",1);
        $page = common::request("p","G",1);
        $city = common::request('city','G');
        if($type == 1){
            $recommend = array_slice($this->load('group')->getGroupByUid($uid),0,10);
            $authGroup = $this->load('group')->getGroupByType(2,$city,$page);
            $gids = array_unique(array_merge($authGroup,$recommend));
        }else{
            $gids = $this->load('group')->getGroupByType($type,$city,$page);
        }
        $gList = [];
        foreach($gids as $key=>$gid){
            $info['gid']= $gid;
            $groupInfo = $this->load("group")->getGroupInfo($gid);
            $info['name'] = $groupInfo['name'];
            $info['header'] = $groupInfo['header'];
            $info['authenticate'] = $this->load('group')->isAuth($gid);
            $activity = $this->load('groupActivity')->getIngAct($gid,0,5);
            $info['isActivity'] = empty($activity) ? 0 : 1;
            $info['totalMileage'] = floatval($this->load('group')->getTotalMileageByGid($gid));
            $info['joinNum'] = $groupInfo['members'];
            $info['city'] = $groupInfo['city'];
            $info['slogan'] = $groupInfo['slogan'];
            $info['status'] = $this->load('group')->getGroupStatus($uid,$gid);
            $info['isverify'] = isset($groupInfo['isverify']) ? intval($groupInfo['isverify']) : 0;
            $gList[] = $info;
        }
        $this->result['result'] = compact('gList');
    }

    /**
     * 我有管理权限的群组
     */
    public function domyManageGroup(){
        $uid = $this->uid;
        if(empty($uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $created = $this->load('group')->myGroupListByRole($uid, 1);
        $manager = $this->load('group')->myGroupListByRole($uid, 2);
        $groups = array_merge($created,$manager);
        $this->result['result'] = compact('groups');
    }



    /**
     * 创建活动
     */
    public function docreateActivity(){
        $title = common::request('title',"P");
        $stime = common::request('atime',"P");
        $etime = common::request('etime',"P");
        $type = common::request('type',"P");
        $place = common::request('place',"P",'');
        $cost = common::request('free',"P",0);
        $way = common::request('join',"P",0);
        $introduce = common::request('introduce',"P");
        $is_show = common::request('is_show','P');
        $gid = common::request('gid',"P");
        $target = common::request('target','P',0);
        $days = common::request('days','P',0);
        $city = common::request("city","P",'');
        $lat = common::request('lat',"P");
        $lng = common::request('lng',"P");
        $address = common::request("address",'P');
        $is_collect_info = common::request("is_collect_info",'P',0);
        $collect_ext_name1 = trim(common::request("collect_ext_name1",'P',''));
        $collect_ext_name2 = trim(common::request("collect_ext_name2",'P',''));
        $uid = $this->uid;
        if(empty($uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        if(empty($title) || empty($stime) || empty($etime) || empty($introduce) || (($type == 2 || $type==3) && empty($target))){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $badwords_mod = $this->load('badwords');
        if($badwords_mod->check($title) || $badwords_mod->check($introduce) || $badwords_mod->check($collect_ext_name1) || $badwords_mod->check($collect_ext_name2))
        {
            $_ENV['error']->getErrors(2008);
            exit;
        }
        $gInfo = $this->load('group')->getGroupInfo($gid);
        if(empty($gInfo) || $gInfo['isDismiss']){
            $_ENV['error']->getErrors(905);
            exit;
        }
        $img = $this->load('qiniu')->uploadQiNiuImg('img', 'ACT/'.md5($title.'/'.time().common::getHashStr(6)));
        $data = compact('uid','gid','title','stime','etime','type','target','img','place','cost','way','introduce','is_show','days','city','lat','lng','address','is_collect_info','collect_ext_name1','collect_ext_name2');
        $aid = $this->load('groupActivity')->addActivity($data);

        if(empty($aid)){
            $_ENV['error']->getErrors(100);
            exit;
        }
        if($type == 2 || $type == 4){
            $this->redis->ZINCRBY(REDIS_PREFIX.'GADay_'.$gid."_".$aid,0,$uid);
        }
        if($type == 1 || $type == 3){
            $this->redis->ZINCRBY(REDIS_PREFIX.'GARecord_'.$gid."_".$aid,0,$uid);
        }
        
        $energySet = common::$energySet;
        $msg = [
            "title" => "太棒啦！",
            "subtitle" => "活动发布成功了",
            "content" => sprintf("扑妞奖励您%d点能量值\n分享此活动您还将获得%d点能量值",$energySet['createActivity']['energy'],$energySet['shareActivity']['energy'])
        ];
        $this->result['result'] = compact('aid','msg');
    }

    /**
     * 活动操作（参加、退出、取消）
     */
    public function dohandleActivity(){
        $type = common::request('type','G');//type 操作类型 1：参加 2：退出 3：取消
        $aid = common::request('aid','G');
        $gid = common::request('gid','G');
        $phone = common::request('phone','G','',true);
        $name = common::request('name','G','',true);
        $gender = common::request('gender','G');
        $collect_ext_value1 = common::request('collect_ext_value1','G');
        $collect_ext_value2 = common::request('collect_ext_value2','G');
        $uid = $this->uid;
        if(empty($uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        if(empty($aid) || empty($gid)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $actInfo = $this->load('groupActivity')->getActByAid($aid);
        if(empty($actInfo)){
            $_ENV['error']->getErrors(2001);
            exit;
        }
        $redisKey = REDIS_PREFIX."GAJoin_".$gid."_".$aid;
        $time=time();
        
        if($type == 1){
            $is_collect_info = false;
            if(isset($actInfo['is_collect_info']) && $actInfo['is_collect_info']){
                $is_collect_info = true;
                if($this->vn < 2860){
                    $_ENV['error']->getErrors(914);
                    exit;
                }
            }
            
            $giftId = isset($actInfo['gift_id']) ? $actInfo['gift_id'] : '';
            if($this->vn < 2730 && $actInfo['type'] > 2 && (!empty($giftId) ||  $actInfo['cost'] > 0)){
                $_ENV['error']->getErrors(914);
                exit;
            }
            $status = $this->load('group')->getGroupStatus($uid,$gid);
            //活动是群内成员，并且该用户非群成员
            if($actInfo['way'] == 0 && $status == 7){
                $_ENV['error']->getErrors(2002);
                exit;
            }
            //活动是群内成员，并且已经申请加群
            if($actInfo['way'] == 0 && $status == 1){
                $_ENV['error']->getErrors(2007);
                exit;
            }

            if($actInfo['way'] == 1 && $status !=0){
                if($this->vn >= 2860){
                    $this->load('group')->joinGroup($gid,$uid);
                    $this->load('group')->sendMessage(array($uid),$gid,'G12',0);
                }else{
                    $gInfo = $this->load('group')->getGroupInfo($gid);
                    if(isset($gInfo['isverify']) && $gInfo['isverify'] == 1){
                        $this->load('group')->joinGroup($gid,$uid);
                        $this->load('group')->sendMessage(array($uid),$gid,'G12',0);
                    }
                }
            }
            
            $userOrder = $this->load('order')->getOrderByAid($uid,$aid);
            if($this->vn>=2730 && $actInfo['type'] > 2 && (isset($userOrder['pay_status']) && $userOrder['pay_status'] != 20 || empty($userOrder))){
                if(empty($name) || empty($phone)){
                    $_ENV['error']->getErrors(101);
                    exit;
                }
                //活动是否在两小时内，两小时内的活动不能参加
//                if(!$this->load('groupActivity')->isHandle($actInfo['stime'])){
//                    $_ENV['error']->getErrors(2009);
//                    exit;
//                }
                //关闭的官方活动不能参加
                if($this->redis->sismember(REDIS_PREFIX."CloseActGroup_".$aid,$gid)){
                    $_ENV['error']->getErrors(2010);
                    exit;
                }
                
                $is_collect_info = true;
                if($actInfo['cost'] < 0){
                    if(!$this->redis->sIsMember($redisKey,$uid)){
                        $sql = "insert into r_activity_add(gid,uid,aid,add_time) values($gid,$uid,$aid,$time)";
                        $this->db->query($sql);
                        $this->redis->sadd($redisKey,$uid);
                    }
                }else{
                    //$this->db->insertRowTable('r_collect_info',['aid'=>$aid,'name'=>$name,'phone'=>$phone,'addtime'=>time(),'uid'=>$uid]);
                    $fee = $actInfo['cost'];
                    $this->in_erpDb();
                    $orderInfo = [
                        'aid'          => $aid,
                        'order_id'     => $this->load('order')->createOrderSn(),
                        'goods_id'     => 0,
                        'num'          => 1,
                        'client_type'  => common::getOs(),
                        'uid'          => $uid,
                        'total_fee'    => $fee,
                        'price'        => $fee,
                        'status'       => 2,
                        'consignee_id' => 0,
                        'addtime'      => time(),
                        'updatetime'   => time(),
                        'gid'          => $gid
                    ];
                    $this->erpDb->insertRowTable('r_order',$orderInfo);
                }
            }else{
                if(!$this->redis->sIsMember($redisKey,$uid)){
                    $sql = "insert into r_activity_add(gid,uid,aid,add_time) values($gid,$uid,$aid,$time)";
                    $this->db->query($sql);
                    $this->redis->sadd($redisKey,$uid);
                }
                $this->load('group')->sendMessage(array($actInfo['uid']),$gid,'G11',0,$aid);
                $atime = $actInfo['stime'];
                if(($actInfo['stime'] + 7200) > time()){
                    $atime = $actInfo['stime']+1000000000;
                }
                if($actInfo['type'] == 2 || $actInfo['type'] == 4){
                    $this->redis->ZINCRBY(REDIS_PREFIX.'GADay_'.$gid."_".$aid,0,$uid);
                }
                if($actInfo['type'] == 1 || $actInfo['type'] == 3){
                    $this->redis->ZINCRBY(REDIS_PREFIX.'GARecord_'.$gid."_".$aid,0,$uid);
                }
                $this->redis->zadd(REDIS_PREFIX."MYActivity_".$uid,$atime,$aid);
            }
            //收集报名信息
            if($is_collect_info){
                $collect_info = compact('aid','uid','phone','name','gender','collect_ext_value1','collect_ext_value2');
                $collect_info['addtime'] = $time;
                $this->db->insertRowTable('r_collect_info',$collect_info);
            }
            
            //添加正能量
            $this->load('client')->userEnergyChange($uid,'joinActivity',$aid);

            //返回弹出提示
            $energySet = common::$energySet;
            $msg = [
                'title' => '恭喜您已参加活动',
                'subtitle' => sprintf('同时获得%s点能量值',$energySet['joinActivity']['energy']),
                'content' => "分享链接邀请好友参与此次活动\n您就有机会获得能量值"
            ];
            
            //发送消息提示
            $this->load('group')->sendMessage(array($uid),$gid,'G23',0,$aid);
        }elseif($type == 2){
            $sql = "delete from r_activity_add where gid=$gid and aid=$aid and uid=$uid";
            $this->db->query($sql);
            $this->redis->sRem($redisKey,$uid);
            $this->load('group')->sendMessage(array($actInfo['uid']),$gid,'G14',0,$aid);
            $this->redis->zrem(REDIS_PREFIX."MYActivity_".$uid,$aid);
        }else{
            $role = $this->load('group')->getGroupRole($gid,$uid);
            if(($role != 1 && $uid != $actInfo['uid'])){
                $_ENV['error']->getErrors(903);
                exit;
            }
            if($actInfo['stime']-time() < 7200){
                $_ENV['error']->getErrors(2004);
                exit;
            }
            $this->load('groupActivity')->cancelActivity($aid,$gid);
        }

        $this->result['result']['is_success'] = 1;
        if(isset($msg)){
            $this->result['result']['msg'] = $msg;
        }
    }

    /**
     * 活动详情
     */
    public function dodetail(){
        $aid = common::request('aid','G');
        $gid = common::request('gid','G',0);
        $uid = $this->uid;
        if(empty($aid) || empty($gid)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $info = $this->load('groupActivity')->getActByAid($aid);
        $groupInfo = $this->load('group')->getGroupInfo($gid);
        $joins = array();
        $nickName = '';
        $header = '';
        $role = $this->load('group')->getGroupRole($gid,$uid);
        //非官方活动
        if($info['type'] < 3){
            $userInfo = $this->load('client')->getUserInfo($info['uid']);
            $nickName = $userInfo['nickname'];
            $header = $userInfo['header'];
        }
        $isClaim = intval($this->redis->sismember(REDIS_PREFIX."ACTaccept_".$info['aid'],$gid));
        $join = array_slice($this->redis->smembers(REDIS_PREFIX."GAJoin_".$gid."_".$aid),0,8);
        $joinNum = $this->redis->ssize(REDIS_PREFIX."GAJoin_".$gid."_".$aid);
        $isNotice = intval($this->redis->sismember(REDIS_PREFIX."GAJoin_".$gid."_".$aid,$uid));
        if($this->vn >= 2730 && !$isClaim && $info['type'] > 2){
            $join = $this->load('groupActivity')->getNotices($aid,$gid);
            $joinNum = count($join);
            $join = array_slice($join,0,8);
            $isNotice = $this->load('groupActivity')->isNotice($aid,$uid,$gid);
        }
        foreach($join as $item){
            $id = is_array($item) ? $item['uid'] : $item;
            $user = $this->load('client')->getUserInfo($id);
            $uinfo['header'] = $user['header'];
            $uinfo['uid'] = $id;
            $joins[] = $uinfo;
        }
        $activity = $this->load('groupActivity')->getIngAct($gid,$aid,3);
        $actList = array();
        foreach($activity as $k=>$val){
            $actList[$k]['aid'] = $val['aid'];
            $actList[$k]['title'] = $val['title'];
            $actList[$k]['type'] = $val['type'];
        }

        $comment = $this->load('groupActivity')->getCommend($aid);
        $commentList = array();
        foreach($comment as $k=>$data){
            $user = $this->load('client')->getUserInfo($data['uid']);
            $commentList[$k]['cid'] = $data['cid'];
            $commentList[$k]['uid'] = $data['uid'];
            $commentList[$k]['content'] = $data['content'];
            $commentList[$k]['add_time'] = $data['add_time'];
            $commentList[$k]['name'] = $user['nickname'];
            $commentList[$k]['header'] = $user['header'];
        }
        $orderInfo = $this->load('order')->getOrderByAid($uid,$aid);
        $trends = $groupRank = [];
        $curGroup = (object)[];
        $isGoods = $goodsId = $payStauts = 0;
        $cost = $info['cost'];
        if(in_array($info['type'],[3,4,5]) && $this->vn >= 2730){
            $trends = $this->load('groupActivity')->trend($aid);
            $groupRank = $this->load('groupActivity')->groupRank($aid);
            $userOrder = $this->load('order')->getOrderByAid($uid,$aid);
            if(isset($userOrder['pay_status']) && $userOrder['pay_status'] == 20){
                $payStauts = 1;
            }
            if(isset($info['gift_type']) && $info['gift_type'] == 2){
                $goodsId = $info['gift_id'];
                $isGoods = 1;
            }
            if($this->redis->sismember(REDIS_PREFIX."ACTaccept_".$aid,$gid)){
                $curGroup = [
                    'gid' => $gid,
                    'name' => $groupInfo['name'],
                    'header' => $groupInfo['header'],
                    'rank' => intval($this->redis->zrevrank(REDIS_PREFIX.'GroupAct_'.$aid,$gid)) + 1,
                    'mileage' => strval($this->redis->zscore(REDIS_PREFIX.'GroupAct_'.$aid,$gid))
                ];
            }


        }
        $statist = $this->load('groupActivity')->actStatist($gid,$aid);//统计数据
        $members = 0;
        if($this->vn >= 2730){
            $members = $statist['members'];
        }
        $shareToken = base64_encode($uid.'_'.$aid);
        $data = array(
            "role"        =>  $role,
            'title'       =>  $info['title'],
            "aid"         =>  $info['aid'],
            "gid"         =>  $gid,
            "uid"         =>  $info['uid'],
            "type"        =>  intval($info['type']),
            'isJoin'      =>  $this->load('groupActivity')->isJoin($aid,$uid,$gid),
            'place'       =>  $info['place'],
            "stime"       =>  $info['stime'],
            "etime"       =>  $info['etime'],
            "img"         =>  $info['img'],
            "cost"        =>  $cost,
            "introduce"   =>  $info['introduce'],
            "name"        =>  $nickName,
            'isClaim'     =>  $isClaim,
            "header"      =>  $header,
            "joins"       =>  $joins,
            "gName"       =>  isset($groupInfo['name']) ? $groupInfo['name'] : '',
            "joinNum"     =>  $joinNum,
            "days"        =>  $info['days'],
            "way"         =>  intval($info['way']),
            "isverify"    =>  isset($groupInfo['isverify']) ? intval($groupInfo['isverify']) : 0,
            "target"      =>  number_format($info['target'],2,'.',''),
            'groupStatus' =>  $this->load('group')->getGroupStatus($uid,$gid),
            'status'      =>  $info['status'] != 1 ? 0 : $info['status'],
            'url'         =>  $info['url'],
            "activity"    =>  $actList,
            "comment"     =>  $commentList,
            "isNotice"    =>  $isNotice,
            'lat'         =>  isset($info['lat']) ? $info['lat'] : '',
            "lng"         =>  isset($info['lng']) ? $info['lng'] : '',
            "address"     =>  isset($info['address']) ? $info['address'] : '',
            'curUser'     =>  $statist['curUser'],//当前用户统计数据
            'members'     =>  $members, //成员统计数据
            'trends'      =>  $trends, //动态
            'rule'        =>  isset($info['rule']) ? $info['rule'] : '' ,//活动规则
            'banner'      =>  isset($info['banner']) ? $info['banner'] : [],//活动banner
            'curTime'     =>  time(),//当前时间
            'groupRank'   =>  $groupRank,//群组排行
            'orderStatus' =>  isset($orderInfo['order_id']) ? $orderInfo['order_id'] : '',
            'curGroup'    =>  $curGroup,
            'isGoods'     =>  $isGoods,
            'goodsId'     =>  $goodsId,
            'payStatus'   =>  $payStauts, //支付状态
            'cost_content'=>  isset($info['cost_content']) ? $info['cost_content'] : '', //费用介绍
            'shareUrl'    =>  'http://'.$this->config['host']."/show/newDetail?gid=$gid&aid=$aid&shareToken=$shareToken",
            'is_collect_info' => isset($info['is_collect_info']) ? $info['is_collect_info'] : 0,
            'collect_ext_name1' => isset($info['collect_ext_name2']) ? $info['collect_ext_name1'] : '',
            'collect_ext_name2' => isset($info['collect_ext_name2']) ? $info['collect_ext_name2'] : ''
        );
        $this->result['result'] = $data;
    }

    /**
     * 活动成员列表
     */
    public function doactivityMembers(){
        $gid = common::request('gid','G');
        $aid = common::request('aid',"G");
        if(empty($gid) || empty($aid)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $id = common::request('id',"G");
        $direc = common::request('direc','G','next');
        $offset = common::request('offset','G',20);
        $type = $this->redis->hget(REDIS_PREFIX."ActivityInfo_".$aid,'type');
        if($this->vn >= 2730 && $type>2 && !$this->redis->sismember(REDIS_PREFIX."ACTaccept_".$aid,$gid)){
            $members = [];
            $notices = $this->load('groupActivity')->getNotices($aid,$gid);
            foreach($notices as $item){
                $members[]['uid'] = $item;
            }
        }else{
            $members = $this->load('groupActivity')->getActJoins($gid,$aid,$id,$direc,$offset);
        }
        foreach($members as &$info){
            $userInfo = $this->load('client')->getUserInfo($info['uid']);
            $info['nickname'] = $userInfo['nickname'];
            $info['header'] = $userInfo['header'];
            $info['gender'] = $userInfo['gender'];
            $info['total_rate'] = $userInfo['total_rate'];
            $info['total_mileage'] = $userInfo['total_mileage'];
            $info['levelInfo'] = $this->load('client')->getUserLevelInfo($info['uid']);
        }
        $this->result['result'] = compact('members');
    }

    /**
     * 编辑活动
     */
    public function doeditActivity(){
        $aid = common::request('aid',"P");
        $title = common::request('title','P');
        $introduce = common::request("introduce","P");
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        if(empty($aid)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $data = array();
        if(!empty($title)){
            $data = compact('aid','title');
            $this->redis->hset(REDIS_PREFIX.'ActivityInfo_'.$aid,'title',$title);
            $this->db->updateRowTable("r_group_activity",$data);
            $this->load('xunSearch')->init('activity')->updateIndex($data);
        }
        if(!empty($introduce)){
            $data = compact('aid','introduce');
            $this->redis->hset(REDIS_PREFIX.'ActivityInfo_'.$aid,'introduce',$introduce);
            $this->db->updateRowTable("r_group_activity",$data);
        }
        if(empty($data)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $this->result['result']['is_success'] = 1;
    }

    /**
     * 活动评论列表
     */
    public function docommentList(){
        $cid = common::request('cid','G','0');
        $direc = common::request('direc',"G","next");
        $offset = common::request('offset','G',20);
        $aid = common::request('aid','G');
        if(empty($aid)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $info = $this->load('groupActivity')->getCommend($aid,$cid,$direc,$offset);
        $commentList = array();
        foreach($info as $k=>$item){
            $userInfo = $this->load('client')->getUserInfo($item['uid']);
            $commentList[$k]['cid'] = $item['cid'];
            $commentList[$k]['uid'] = $item['uid'];
            $commentList[$k]['content'] = $item['content'];
            $commentList[$k]['add_time'] = $item['add_time'];
            $commentList[$k]['name'] = $userInfo['nickname'];
            $commentList[$k]['header'] = $userInfo['header'];
            $commentList[$k]['comment_img'] = $item['comment_img'];
            $commentList[$k]['thumb_img'] = $item['thumb_img'];
        }
        $comment = $commentList;
        $this->result['result'] = compact('comment');
    }

    /**
     * 活动评论
     */
    public function dopublishComment(){
        $aid = common::request('aid',"P");
        $content = common::request('content',"P");
        $father_id = common::request('father_id',"P",0);
        $gid = common::request('gid',"P");
        $uid = $this->uid;
        if(empty($uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        if(empty($content) || empty($aid)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        if($this->load('badwords')->check($content))
        {
            $_ENV['error']->getErrors(605);
            exit;
        }
        $info = $this->load('groupActivity')->getActByAid($aid);
        $add_time = time();
        $comment_img = $this->load('qiniu')->uploadQiNiuImg('comment_img', 'PC/'.md5($aid.'/'.time().common::getHashStr(6)));
        $data = compact('uid','father_id','add_time','content','aid','comment_img');
        $comment = $this->load('groupActivity')->addComment($data);
        if($father_id > 0){
            $father_uid = $this->db->getone("select uid from r_activity_comment where aid=$aid and cid = {$father_id} limit 1");
            $this->load('bbs')->sendMessage($this->uid,$father_uid,$aid,$father_id,$content,'N6',$gid);
        }else{
            $this->load('bbs')->sendMessage($this->uid,$info['uid'],$aid,'',$content,'N5',$gid);
        }
        $this->result['result'] = compact('comment');
    }

    /**
     * 群空间
     */
    public function dogroupRoom(){
        $gid = common::request('gid','G');
        if(empty($gid)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $uid = $this->uid;
        if(empty($uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $gInfo = $this->load('group')->getGroupInfo($gid);
        $role = $this->load('group')->getGroupRole($gid, $uid);
        $data = array("gid"=>$gid,"gname"=>$gInfo['name'],"role"=>$role);
        $data['pNum'] = $this->redis->zsize(REDIS_PREFIX.'GroupNews_'.$gid);
        $data['actNum'] = intval($this->redis->zsize(REDIS_PREFIX."GroupActivity_".$gid));//活动数量
        $activityList = $this->load('groupActivity')->getGRoomAct($gid,10);
        $activity = array();
        $isClaim = 0;
        $members = [];
        foreach($activityList as $k=>$info){
            $count = count($activity);
            if($count >= 2){
                break;
            }
            $joins = [];
            if($info['type'] > 2){
                //关闭的官方活动不显示
                if(
                    ((in_array($gid,$this->redis->smembers(REDIS_PREFIX."CloseActMembers_".$info['aid']."_".$uid)) || $this->redis->sismember(REDIS_PREFIX."CloseActGroup_".$info['aid'],$gid))
                        || (time() > $info['stime'] && !$this->redis->sIsMember(REDIS_PREFIX."GAJoin_".$gid."_".$info['aid'],$uid))
                        && $this->vn >= 2730)
                    || (($info['cost'] > 0 || (isset($info['gift_id']) && $info['gift_id'] > 0)) && $this->vn < 2730)
                ){
                    continue;
                }
                $isClaim = intval($this->redis->sismember(REDIS_PREFIX."ACTaccept_".$info['aid'],$gid));
                if($this->vn >= 2730 && !$isClaim){
                    $members = $this->load('groupActivity')->getNotices($info['aid'],$gid);
                }else{
                    $members = $this->redis->smembers(REDIS_PREFIX."GAJoin_".$gid."_".$info['aid']);
                }
                $join = array_slice($members,0,7);
                foreach($join as $item){
                    $id = is_array($item) ? $item['uid'] : $item;
                    $user = $this->load('client')->getUserInfo($id);
                    $uinfo['header'] = $user['header'];
                    $uinfo['uid'] = $id;
                    $joins[] = $uinfo;
                }

            }
            $alist['type'] = $info['type'];
            $alist['gid'] = $gid;
            $alist['aid'] = $info['aid'];
            $alist['title'] = $info['title'];
            $alist['place'] = $info['place'];
            $alist['stime'] = $info['stime'];
            $alist['etime'] = $info['etime'];
            $alist['cost'] = $info['cost'];
            $alist['img'] = $info['img'];
            $alist['status'] = $info['status'];
            $alist['isNotice'] = intval($this->redis->sismember(REDIS_PREFIX."GAJoin_".$gid."_".$info['aid'],$uid));
            $alist['isClaim'] = $isClaim;
            $alist['claimGroup'] = intval($this->redis->ssize(REDIS_PREFIX."ACTaccept_".$info['aid']));
            $alist['wantJoin'] = count($members);
            $alist['isJoin'] = $this->load('groupActivity')->isJoin($info['aid'],$uid,$gid);
            $alist['joinNum'] = $this->load('groupActivity')->joinNum($gid,$info['aid']);
            $alist['isClose'] = intval($this->redis->sismember(REDIS_PREFIX."CloseActGroup_".$info['aid'],$gid));//群主是否关闭官方活动
            $alist['subTitle'] = $this->redis->hget(REDIS_PREFIX."ActivityInfo_".$info['aid'],'short_desc');
            $alist['userClose'] = intval(in_array($gid,$this->redis->smembers(REDIS_PREFIX."CloseActMembers_".$info['aid']."_".$uid)));//用户是否关闭官方活动
            $alist['curTime'] = time();
            $alist['joins'] = $joins;
            $alist['target'] = $info['target'];
            $alist['days'] = $info['days'];
            $activity[] = $alist;
        }
        $topId = $this->load('group')->getTopNews($gid);
        $topInfo = array();
        if($topId){
            $data['pNum'] += 1;
            $topInfo[] = $this->_reData($topId,1);
        }
        $news_id = $this->load('group')->getPostByGid($gid);
        $postList = array();
        foreach($news_id as $info){
            $newsId = is_array($info) ? $info['news_id'] : $info;
            $postList[] = $this->_reData($newsId);
        }

        $data['postList'] = array_merge($topInfo,$postList);
        $data['activity'] = $activity;
        $this->result['result'] = $data;
    }

    /**
     * 群帖子置顶/取消
     */
    public function doisTop(){
        $news_id = common::request('news_id','G');
        $gid = common::request('gid',"G");
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $newsInfo = $this->load('group')->getNews($news_id,$gid);
        if(empty($newsInfo) || empty($gid)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        //$gid = $newsInfo['gid'];
        $role = $this->load('group')->getGroupRole($gid,$this->uid);
        if($role !=1){
            $_ENV['error']->getErrors(903);
            exit;
        }
        $isTop = $newsInfo['is_top'];
        $top = ($isTop == 1) ? 0 : 1;
        $key = REDIS_PREFIX.'GroupNews_'.$gid;
        if($top == 1){
            $this->redis->zrem($key,$news_id);
            $id = $this->load('group')->getTopNews($gid);
            $info = $this->load('group')->getNews($id,$gid);
            if(isset($info['is_show']) && $info['is_show'] == 1){
                $this->redis->zadd($key,$info['addtime'],$id);
            }
            $this->load('bbs')->sendMessage($this->uid,$newsInfo['uid'],$news_id,'','','N7');
        }else{
            $this->redis->zadd($key,$newsInfo['addtime'],$news_id);
        }
        $this->db->query("update r_group_news set is_top=$top where news_id=$news_id and gid=$gid");
        $this->db->query("update r_group_news set is_top=0 where news_id!=$news_id and gid=$gid");
        $this->result['result']['is_success'] = 1;
    }

    /**
     * 删除群帖子
     */
    public function dodelPost(){
        $uid = $this->uid;
        if(empty($uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $news_id = common::request('news_id','G');
        $gid = common::request('gid','G');
        $newsInfo = $this->load('bbs')->getPostInfo($news_id);
        if(empty($newsInfo) || empty($gid)){
            $_ENV['error']->getErrors(101);
            exit;
        }

        $host = $this->load('group')->getGroupRole($gid, $uid);
        $newsInfo = $this->load('group')->getNews($news_id,$gid);
        if($newsInfo['is_top'] == 1 && $host !=1 ){
            $_ENV['error']->getErrors(2003);
            exit;
        }
        if( $host != 1 && $uid != $newsInfo['uid']){
            $_ENV['error']->getErrors(903);
            exit;
        }
        if($uid == $newsInfo['uid']){
            $this->load('bbs')->hidePost($uid, $news_id);
        }
        $this->redis->zrem(REDIS_PREFIX.'GroupNews_'.$gid,$news_id);
        $this->load('bbs')->sendMessage($this->uid,$newsInfo['uid'],$news_id,'','','N8');
        $this->db->query("update r_group_news set is_show=0 where news_id=$news_id");

        $this->result['result']['is_success'] = 1;
    }

    /**
     * 群组帖子列表
     */
    public function dogroupPostList(){
        $gid = common::request('gid','G');
        $news_id = common::request('news_id','G',0);
        $direc = common::request('direc','G','next');
        $offset = common::request('offset','G',20);
        if(empty($gid)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $ids = $this->load('group')->getPostByGid($gid,$news_id,$direc,$offset);
        $postlist = [];
        foreach($ids as $info){
            $newsId = is_array($info) ? $info['news_id'] : $info;
            $postInfo = $this->load('bbs')->getPostInfo($newsId);
            $postInfo['block'] = array();
            $loveList = $this->load('bbs')->getPostLikeList($newsId,0,'next',8);
            if($loveList)
            {
                foreach ($loveList as $key => $v)
                {
                    unset($loveList[$key]['level'],$loveList[$key]['nickname'],$loveList[$key]['gender'],$loveList[$key]['total_rate'],$loveList[$key]['total_mileage']);
                }
            }
            $postInfo['imginfo'] = empty($postInfo['imginfo']) ? array() : json_decode($postInfo['imginfo'],true);
            $postInfo['loveList'] = empty($loveList) ? array() : $loveList;
            // 评论列表
            $postInfo['comments'] = $this->load('bbs')->getCommentsNoImg($newsId);
            $postInfo['isLove'] = $this->redis->ZSCORE(REDIS_PREFIX.'PostLikeList_'.$newsId, $this->uid) === false ? 0 : 1;
            $postInfo['header'] = $this->redis->hget(REDIS_PREFIX.'UserInfo_'.$postInfo['uid'], 'header');
            $postInfo['middle_img'] = $this->load('bbs')->getPostMidImg($postInfo['sourceImg']);
            $postInfo['source_img'] = $postInfo['sourceImg'];
            // 图文混排类型的帖子，图片只显示一张
            if($postInfo['type'] == 1)
            {
                $postInfo['thumb_img'] = !empty($postInfo['thumb_img']) ? array($postInfo['thumb_img'][0]) : array();
                $postInfo['middle_img'] = !empty($postInfo['middle_img']) ? array($postInfo['middle_img'][0]) : array();
                $postInfo['source_img'] = !empty($postInfo['source_img']) ? array($postInfo['source_img'][0]) : array();
                $postInfo['imginfo'] = !empty($postInfo['imginfo']) ? array($postInfo['imginfo'][0]) : array();
            }
            $postlist[] = $postInfo;
        }
        $this->result['result'] = compact('postlist');
    }

    /**
     * 搜索群组/帖子
     */
    public function dosearch(){
        $keywords = common::request('keywords',"G");
        $type = common::request('type',"G",0);
        $page = common::request('p','G',1);
        $offset = common::request('offset','G',20);
        $uid = $this->uid;
        $offset = $type == 0 ? 6 : $offset;
        if(preg_match("/\d{5,6}/",$keywords)){
            $ids = $this->load('xunSearch')->init('group')->search("{$keywords}",'id',$offset,$page);//群组列表
            foreach($ids as $item) {
                if (isset($item['gid']) && $item['gid'] == $keywords) {
                    $gids = array(array("gid"=>$keywords));

                    if($this->load('group')->getGroupStatus($uid,$keywords) == 0){
                        $actIds = $this->load('groupActivity')->getActList($keywords,0,'next',20);
                    }else{
                        $actIds = $this->load('groupActivity')->getActList($keywords,0,'next',10);
                    }
                } else {
                    $gids = $ids;
                }
            }
        }else{
            $gids = $this->load('xunSearch')->init('group')->search("{$keywords}",'id',$offset,$page);//群组列表
            $actIds = $this->load('xunSearch')->init('activity')->search("{$keywords}",'aid',$offset,$page);//活动列表
        }
        $groups = $activity = array();
        if(!empty($gids)){
            foreach($gids as $info){
                $gInfo = $this->load('group')->getGroupInfo($info['gid']);
                $group['gid'] = $info['gid'];
                $group['name'] = $gInfo['name'];
                $group['header'] = $gInfo['header'];
                $group['status'] = $this->load('group')->getGroupStatus($uid,$info['gid']);
                $actData = $this->load('groupActivity')->getIngAct($info['gid'],0,1);
                $group['isActivity'] =  empty($actData) ? 0 : 1;
                $group['totalMileage'] = floatval($this->load('group')->getTotalMileageByGid($info['gid']));
                $group['joinNum'] = $gInfo['members'];
                $group['authenticate'] = $this->load('group')->isAuth($info['gid']);
                $group['isverify'] = isset($gInfo['isverify']) ? intval($gInfo['isverify']) : 0;
                $group['role'] = $this->load('group')->getGroupRole($info['gid'], $uid);
                $group['slogan'] = $gInfo['slogan'];
                $groups[] = $group;
            }
        }
        if(!empty($actIds)){
            foreach($actIds as $val){
                if(is_array($val)){
                    $id = $val['aid'];
                }else{
                    $id = $val;
                }
                $aInfo = $this->load('groupActivity')->getActByAid($id);
                if($aInfo['type'] > 2){
                    continue;
                }
                $gInfo = $this->load('group')->getGroupInfo($aInfo['gid']);
                $act['aid'] = $aInfo['aid'];
                $act['gid'] = $aInfo['gid'];
                $act['title'] = $aInfo['title'];
                $act['type'] = intval($aInfo['type']);
                $act['place'] = $aInfo['place'];
                $act['stime'] = $aInfo['stime'];
                $act['etime'] = $aInfo['etime'];
                $act['joinNum'] = $this->load('groupActivity')->joinNum($aInfo['gid'],$id);
                $act['isJoin'] = $this->load('groupActivity')->isJoin($id,$uid,$aInfo['gid']);
                $act['img'] = $aInfo['img'];
                $act['header'] = isset($gInfo['header']) ? $gInfo['header'] : '';
                $act['name'] = isset($gInfo['name']) ? $gInfo['name'] : '';
                $act['status'] = intval($aInfo['status']) != 1 ? 0 : $aInfo['status'];
                $act['curTime'] = time();
                $act['cost'] = $aInfo['cost'];
                $act['days'] = $aInfo['days'];
                $act['target'] = $aInfo['target'];
                $activity[] = $act;

            }
        }
        if($type == 0){
            $data = compact('activity','groups');
        }elseif($type == 1){
            $data['groups'] = $groups;
        }else{
            $data["activity"] = $activity;
        }

        $this->result['result'] = $data;
    }

    /**
     * 通知群主
     */
    public function donotice(){
        $gid = common::request('gid','G');
        $aid = common::request('aid','G');
        $phone = common::request('phone','G');
        $name = common::request('name','G');
        if(empty($gid) || empty($aid)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $uid = $this->uid;
        if(empty($uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $actInfo = $this->load('groupActivity')->getActByAid($aid);
        $atime = $actInfo['stime'];
        $giftId = isset($actInfo['gift_id']) ? $actInfo['gift_id'] : '';
        if($this->vn < 2730 && $actInfo['type'] > 2 && (!empty($giftId) ||  $actInfo['cost'] > 0)){
            $_ENV['error']->getErrors(914);
            exit;
        }
        $order_id = '';
        $hosts = $this->redis->hget(REDIS_PREFIX.'GroupHost',$gid);
        $isnotice = $this->load('groupActivity')->isNotice($aid,$uid,$gid);
        $isClaim = $this->redis->sismember(REDIS_PREFIX."ACTaccept_".$aid,$gid);
        if($this->vn >= 2730){
            if($isClaim || $isnotice ){
                $userOrder = $this->load('order')->getOrderByAid($uid,$aid);
                $this->db->insertRowTable('r_collect_info',['aid'=>$aid,'name'=>$name,'phone'=>$phone,'addtime'=>time(),'uid'=>$uid]);
                if($actInfo['cost'] <= 0 || !empty($userOrder)){
                    $redisKey = REDIS_PREFIX."GAJoin_".$gid."_".$aid;//参加活动
                    if($actInfo['type'] == 2 || $actInfo['type'] == 4){
                        $this->redis->ZINCRBY(REDIS_PREFIX.'GADay_'.$gid."_".$aid,0,$uid);
                    }
                    if($actInfo['type'] == 1 || $actInfo['type'] == 3){
                        $this->redis->ZINCRBY(REDIS_PREFIX.'GARecord_'.$gid."_".$aid,0,$uid);
                    }
                    $this->redis->zadd(REDIS_PREFIX."MYActivity_".$uid,$atime,$aid);
                    $hosts = $this->redis->hget(REDIS_PREFIX.'GroupHost',$gid);
                    intval($this->redis->ssize($redisKey)) <=0 && $this->load('group')->sendMessage(array($hosts),$gid,'G15',0,$aid);
                    if(!$this->redis->sIsMember($redisKey,$uid)){
                        $this->redis->sadd($redisKey,$uid);
                        $time = time();
                        $sql = "insert into r_activity_add(gid,uid,aid,add_time) values($gid,$this->uid,$aid,$time)";
                        $this->db->query($sql);
                    }
                }else{
                    $fee = $actInfo['cost'];
                    $this->in_erpDb();
                    $order_id = $this->load('order')->createOrderSn();
                    $orderInfo = [
                        'aid'          => $aid,
                        'order_id'     => $order_id,
                        'goods_id'     => 0,
                        'num'          => 1,
                        'client_type'  => common::getOs(),
                        'uid'          => $uid,
                        'total_fee'    => $fee,
                        'price'        => $fee,
                        'status'       => 1,
                        'consignee_id' => 0,
                        'addtime'      => time(),
                        'updatetime'   => time(),
                        'gid'          => $gid
                    ];
                    $this->erpDb->insertRowTable('r_order',$orderInfo);
                }
            }else{
                $this->load('groupActivity')->addNotice($aid,$uid,$gid);//通知群组
            }
        }else{
            $redisKey = REDIS_PREFIX."GAJoin_".$gid."_".$aid;//参加活动
            $this->redis->zadd(REDIS_PREFIX."MYActivity_".$uid,$atime,$aid);
            intval($this->redis->ssize($redisKey)) <=0 && $this->load('group')->sendMessage(array($hosts),$gid,'G15',0,$aid);
            if(!$this->redis->sIsMember($redisKey,$uid)){
                $this->redis->sadd($redisKey,$uid);
                $time = time();
                $sql = "insert into r_activity_add(gid,uid,aid,add_time) values($gid,$this->uid,$aid,$time)";
                $this->db->query($sql);
            }
        }
        $data = [
            'is_success' => 1,
            'order_id'   => $order_id
        ];
        $this->result['result'] = $data;
    }

    /**
     * 活动认领
     */
    public function doaccept(){
        $gid = common::request('gid','G');
        $aid = common::request('aid','G');
        if(empty($gid) || empty($aid)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $actInfo = $this->load('groupActivity')->getActByAid($aid);
//        if(!$this->load('groupActivity')->isHandle($actInfo['stime'])){
//            $_ENV['error']->getErrors(2009);
//            exit;
//        }
        $giftId = isset($actInfo['gift_id']) ? $actInfo['gift_id'] : '';
        if($this->vn < 2730 && $actInfo['type'] > 2 && (!empty($giftId) ||  $actInfo['cost'] > 0)){
            $_ENV['error']->getErrors(914);
            exit;
        }
        $redisKey = REDIS_PREFIX."ACTaccept_".$aid; //活动认领群id
        $this->redis->sadd($redisKey,$gid);
        $time = time();
        $rtime = $actInfo['stime'] + 1000000000;
        if($actInfo['etime'] <= $time){
            $rtime = 1;
        }
        if($actInfo['stime'] <= $time && $time < $actInfo['etime']){
            $rtime = $actInfo['stime'];
        }
        if($this->vn < 2730){
            if(!$this->redis->sIsMember(REDIS_PREFIX."GAJoin_".$gid."_".$aid,$this->uid)){
                $this->redis->sadd(REDIS_PREFIX."GAJoin_".$gid."_".$aid,$this->uid);
                $sql = "insert into r_activity_add(gid,uid,aid,add_time) values($gid,$this->uid,$aid,$time)";
                $this->db->query($sql);
            }
            $this->redis->zadd(REDIS_PREFIX."MYActivity_".$this->uid,$rtime,$aid);
        }
        if($this->vn >= 2730){
            $this->load('groupActivity')->delNotice($aid,$gid);
        }
        $this->redis->zadd(REDIS_PREFIX."GroupActivity_".$gid,$rtime,$aid);
        $this->redis->zadd(REDIS_PREFIX."GroupAct_".$aid,0,$gid);
        $userInfo = $this->load('client')->getUserInfo($this->uid);
        $nickname = isset($userInfo['nickname']) ? $userInfo['nickname'] : "群主";
        $content = $nickname." 参加了 ".$actInfo['title'];
        $this->load('message')->insertMessage(5,$this->uid,$gid,$this->uid,'InfoNtf',$content);
        $this->result['result']['is_success'] = 1;
    }


    /**
     * 群转让
     */
    public function dogroupTransfer(){
        $gid = common::request('gid','G');
        $auid = common::request('uid','G');
        $uid = $this->uid;
        if(empty($uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        if(empty($gid) || empty($auid)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        if($this->load('group')->getGroupRole($gid, $uid) != 1){
            $_ENV['error']->getErrors(903);
            exit;
        }
        $status = $this->load('group')->getGroupStatus($auid, $gid);
        if($status != 0){
            $_ENV['error']->getErrors(911);
            exit;
        }
        $result = $this->load('groupActivity')->isQuit($gid,$uid);
        if($result){
            $_ENV['error']->getErrors(2006);
            exit;
        }
        $this->load('groupActivity')->quitAct($uid,$gid);
        //检测被转让者可创建群数量
        $availeGroupNum = $this->load('group')->getVaileGroupNum($auid);
        if($availeGroupNum == 0)
        {
            $_ENV['error']->getErrors(913);
            exit;
        }
        $this->load('group')->transfer($gid,$auid,$uid);
        $this->result['result']['is_success'] = 1;
    }

    public function doauthGroup(){
        $uid = $this->uid;
        if(empty($uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $gid = common::request('gid',"P");
        $realname = common::request('name',"P");
        $phone = common::request('phone',"P");
        $card = common::request('card',"P");
        $info = common::request('info','P');
        if(empty($gid) || empty($realname) || empty($phone) || empty($phone) || empty($card) || empty($info)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $img = array();
        for($i=0;$i<=1;$i++){
            if (isset($_FILES["img_".$i]) && $_FILES['img_'.$i]['error'] == 0 && in_array($_FILES['img_'.$i]['type'], array('image/png', 'image/gif', 'image/jpeg')))
            {
                $name = md5(time().$i.common::getHashStr(5));
                $imgUrl = $this->load('qiniu')->uploadQiNiuImg('img_'.$i, $name);
                $img[] = $imgUrl;
            }
        }
        $img = json_encode($img);
        $add_time = time();
        $data = compact('uid','gid','realname','phone','card','info','img','add_time');
        $info = $this->load('group')->getAuthGroup($gid);
        if(!empty($info)){
            $data['id'] = $info['id'];
        }
        $this->db->updateRowTable('r_group_auth',$data);
        $this->result['result']['is_success'] = 1;
    }

    //上传证件照
    public function doupImg(){
        $oid = common::request("oid","P");
        if(empty($oid)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $img = array();
        for($i=0;$i<=1;$i++){
            if (isset($_FILES["img_".$i]) && $_FILES['img_'.$i]['error'] == 0 && in_array($_FILES['img_'.$i]['type'], array('image/png', 'image/gif', 'image/jpeg')))
            {
                $name = md5(time().$i.common::getHashStr(5));
                $imgUrl = $this->load('qiniu')->uploadQiNiuImg('img_'.$i, $name);
                $img[] = $imgUrl;
            }
        }
        if(!empty($img)){
            $this->redis->set(REDIS_PREFIX."AuthImg_".$oid,json_encode($img));
        }
        $this->result['result']['img'] = $img;

    }

    /**
     * 组装帖子信息
     * @param $postId
     * @param int $isTop
     * @return mixed
     */
    private function _reData($postId,$isTop=0){
        $postInfo = $this->load('bbs')->getPostInfo($postId);
        $postInfo['block'] = array();
        if($isTop == 1){
            $postInfo['block'] = array(array(
                'title'=>'置顶',
                'color'=>'#FF0000'
            ));
        }
        $loveList = $this->load('bbs')->getPostLikeList($postId,0,'next',8);
        if($loveList)
        {
            foreach ($loveList as $key => $v)
            {
                unset($loveList[$key]['level'],$loveList[$key]['nickname'],$loveList[$key]['gender'],$loveList[$key]['total_rate'],$loveList[$key]['total_mileage']);
            }
        }
        $postInfo['imginfo'] = empty($postInfo['imginfo']) ? array() : json_decode($postInfo['imginfo'],true);
        $postInfo['loveList'] = empty($loveList) ? array() : $loveList;
        // 评论列表
        $postInfo['comments'] = $this->load('bbs')->getCommentsNoImg($postId);
        $postInfo['isLove'] = $this->redis->ZSCORE(REDIS_PREFIX.'PostLikeList_'.$postId, $this->uid) === false ? 0 : 1;
        $postInfo['header'] = $this->redis->hget(REDIS_PREFIX.'UserInfo_'.$postInfo['uid'], 'header');
        $postInfo['middle_img'] = $this->load('bbs')->getPostMidImg($postInfo['sourceImg']);
        $postInfo['source_img'] = $postInfo['sourceImg'];
        // 图文混排类型的帖子，图片只显示一张
        if($postInfo['type'] == 1)
        {
            $postInfo['thumb_img'] = !empty($postInfo['thumb_img']) ? array($postInfo['thumb_img'][0]) : array();
            $postInfo['middle_img'] = !empty($postInfo['middle_img']) ? array($postInfo['middle_img'][0]) : array();
            $postInfo['source_img'] = !empty($postInfo['source_img']) ? array($postInfo['source_img'][0]) : array();
            $postInfo['imginfo'] = !empty($postInfo['imginfo']) ? array($postInfo['imginfo'][0]) : array();
        }
        $postInfo['content'] = $postInfo['type'] ? $postInfo['short_desc'] : $postInfo['content'];
        $postInfo['is_paid_energy'] = $this->load('client')->isPostPaidBy($postInfo['news_id'],$this->uid);
        return $postInfo;
    }


    //======================2.7.2新增======================

    /**
     * 证书数据
     */
    public function docertificate(){
        if($this->afterVersion('2.8.7', '2.8.7')){
            $this->_certificate28();
        }else{
            $this->_certificate27();
        }
    }
    
    public function _certificate28(){
        $id = common::request('id','G','',true);
        $aid = common::request('aid','G','',true);
        $gid = common::request('gid','G','',true);
        $uid = $this->uid;
        if(empty($id) && (empty($aid) || empty($gid))){
            $_ENV['error']->getErrors(101);
            exit;
        }
        if(empty($uid)){
            $_ENV['error']->getErrors(201);
            exit;
        }
        
        $activity_mod = $this->load('groupActivity');
        $reData = $id ? $activity_mod->getCertificateInfoById($id) : $activity_mod->getCertificateInfo($aid,$gid,$uid);
        if(!empty($reData)){
            $match_mod = $this->load('match');
            $reData['actInfo'] = $reData['act_type']==0 ? $activity_mod->getActByAid($reData['aid']) : $match_mod->getMatchInfo($reData['aid']);
        }
        $this->result['result'] = empty($reData) ? (object)$reData : $reData;
    }
    
    
    public function _certificate27(){
        $aid = common::request('aid','G','',true);
        $gid = common::request('gid','G','',true);
        $uid = $this->uid;
        if(empty($aid) || empty($gid)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        if(empty($uid)){
            $_ENV['error']->getErrors(201);
            exit;
        }
        if(!$this->load('groupActivity')->isJoin($aid,$uid,$gid)){
            $this->result['result']['data'] = (object)[];
            exit;
        }
        $userInfo = $this->load('client')->getUserInfo($uid);
        $actInfo = $this->load('groupActivity')->getActByAid($aid);
        $groupInfo = $this->load('group')->getGroupInfo($gid);
        $img = "";
        if($actInfo['type'] > 2 && isset($actInfo['gift_type'])){
            if($actInfo['gift_type'] == 1 && $actInfo['gift_id'] > 0){
                $img = "http://".$this->config['host']."/static/backend/medal/".$actInfo['gift_id'].'.png';
            }else{
                $goodsInfo = $this->load('goods')->getGoodsById($actInfo['gift_id']);
                isset($goodsInfo['thumb']) && $img = $goodsInfo['thumb'];
            }
        }
        if($actInfo['type'] == 2 || $actInfo['type'] == 4){//打卡
            $day = $this->load('groupActivity')->getDays($aid,$gid,$uid,$actInfo['target']);
            $mileage = 0 ;
            $data = $this->redis->hgetAll(REDIS_PREFIX."UDPerDay_{$uid}_{$aid}_{$gid}");
            $runData = [];
            foreach($data as $key=>$item){
                $mileage += $item;
                $info['date'] = date('n.d',strtotime($key));
                $info['mileage'] = round($item,2);
                $runData[] = $info;
            }
            if($day >= $actInfo['days']){//完成打卡
                $reData = [
                    'title'     => $actInfo['title'],
                    'nickname'  => $userInfo['nickname'],
                    'mileage'   => $mileage,
                    'day'       => $day,
                    'runData'   => $runData,
                    'groupName' => $groupInfo['name'],
                    'content'   => "时间会证明，汗水从不骗人！",
                    'isFinish'  => 1,
                    'rank'      => 0
                ];
            }else{
                if($day > ($actInfo['days']*0.1) && $day < ($actInfo['days']*0.5)){
                    $content = "成功近在迟尺，越努力越幸运！";
                }else{
                    $content = "失败乃成功之母，坚持定会成功！";
                }
                $reData = [
                    'title'     => $actInfo['title'],
                    'nickname'  => $userInfo['nickname'],
                    'mileage'   =>$mileage,
                    'day'       => $day,
                    'runData'   => $runData,
                    'groupName' => $groupInfo['name'],
                    'content'   => $content,
                    'isFinish'  => 0,
                    'rank'      => 0
                ];
            }
        }else{
            $mileage = floatval($this->redis->zscore(REDIS_PREFIX."GARecord_{$gid}_{$aid}",$uid));
            $rank = $this->redis->zrevrank(REDIS_PREFIX."GARecord_{$gid}_{$aid}",$uid) + 1;
            $target = $actInfo['target'];
            if($mileage >= $target){
                $is_finish = 1;
            }else{
                $is_finish = 0;
            }
            if($rank == 1 || $rank == 2){
                $content = "只要明确方向，全世界都会为你让路！";
            }elseif($rank == 3){
                $content = "平凡的脚步也可以完成伟大的行程！";
            }else{
                $total = $this->redis->zsize(REDIS_PREFIX."GARecord_{$gid}_{$aid}");
                if($rank >= $total*0.67){
                    $content = "别放弃一点机会，坚持定会成功";
                }elseif($rank >= ($total*0.3) && $rank < ($total*0.67)){
                    $content = "青春流汗不留憾，人生就是拼搏！";
                }else{
                    $content = "别放弃一点机会，坚持定会成功";
                }
            }
            $reData = [
                'title'     => $actInfo['title'],
                'nickname'  => $userInfo['nickname'],
                'mileage'   => $mileage,
                'farthest'  => floatval($this->load('run')->getMileageByTime($uid,$actInfo['stime'],$actInfo['etime'])),
                'content'   => $content,
                'groupName' => $groupInfo['name'],
                'isFinish'  => $is_finish,
                'rank'      => $rank
            ];
        }
        $reData['type'] = $actInfo['type'];
        $reData['img'] = $img;
        $reData['cid'] = intval($this->db->getRow("select aid from r_complain where aid=$aid"));
        $this->result['result']['data'] = empty($reData) ? (object)$reData : $reData;
    }

    /**
     * 关闭官方活动
     */
    public function docloseActivity(){
        $uid = $this->uid;
        if(empty($uid)){
            $_ENV['error']->getErrors(201);
            exit;
        }
        $gid = common::request('gid','G','',true);
        $aid = common::request('aid','G','',true);
        $actInfo = $this->load('groupActivity')->getActByAid($aid);
        if(empty($actInfo)){
            $_ENV['error']->getErrors(2001);
            exit;
        }
        if(!in_array($actInfo['type'],[3,4,5])){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $role = $this->load('group')->getGroupRole($gid,$uid);
        if($role == 1 && !$this->redis->sismember(REDIS_PREFIX."CloseActGroup_".$aid,$gid) && !$this->redis->sismember(REDIS_PREFIX."ACTaccept_".$aid,$gid)){
            $this->redis->sadd(REDIS_PREFIX."CloseActGroup_".$aid,$gid);//关闭官方活动的群
            $members = $this->redis->smembers(REDIS_PREFIX."GAJoin_".$gid."_".$aid);
            $this->load('group')->sendMessage($members,$gid,'G20',0,$aid);
        }else{
            $this->redis->sadd(REDIS_PREFIX."CloseActMembers_".$aid."_".$uid,$gid);//关闭官方活动的用户
        }
        $this->result['result']['is_success'] = 1;
    }

    /**
     * 申诉
     */
    public function docomplian(){
        $uid = $this->uid;
        if(empty($uid)){
            $_ENV['error']->getErrors(201);
            exit;
        }
        $desc = common::request('desc','P');
        $aid = common::request('aid','P','',true);
        $userInfo = $this->load('client')->getUserInfo($uid);
        if(empty($aid) || empty($desc)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $name = $userInfo['nickname'];
        $insertId = $this->load('groupActivity')->addComplain($name,$desc,$aid);
        $this->result['result']['is_success'] = $insertId > 0 ? 1 : 0;

    }

    /**
     * 申诉详情页
     */
    public function docomplainDetail(){
        $id = common::request('id','G');
        if(empty($id)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $list = $this->db->getRow("select aid,name,status,result,reason,img from r_complain where id=$id");
        if(!empty($list)){
            $actInfo = $this->load('groupActivity')->getActByAid($list['aid']);
            $list['title'] = $actInfo['title'];
            if(empty($list['img'])){
                $list['img'] = [];
            }else{
                $list['img'] = json_decode($list['img'],true);
            }
            unset($list['aid']);
        }
        $this->result['result'] = $list ? $list : (object)[];
    }

    /**
     * 我的活动
     */
    public function domyActivity(){
        if(!$this->uid){
            $_ENV['error']->getErrors(201);
            exit;
        }
        $aid = common::request('aid','G',0);
        $direc = common::request('direc',"G",'next');
        $offset = common::request('offset','G',20);
        $aids = $this->load('groupActivity')->getMyActivity($this->uid,$aid,$direc,$offset);
        $activity = array();
        foreach($aids as $id){
            $info = $this->load('groupActivity')->getActByAid($id);
            $info['subTitle'] = isset($info['short_desc']) ? $info['short_desc'] : '';
            $info['isJoin'] = 1;
            if(isset($info['aid'])){
                if($info['type'] > 2){
                    $gids = $this->redis->smembers(REDIS_PREFIX."ACTaccept_".$id);
                    foreach($gids as $gid){
                        if($this->load('group')->getGroupStatus($this->uid, $gid) == 0 && $this->redis->sismember(REDIS_PREFIX."GAJoin_".$gid."_".$id,$this->uid)){
                            $info['joinNum'] = $this->load('groupActivity')->joinNum($gid,$id);
                            $info['gid'] = $gid;
                            $info['type'] = intval($info['type']);
                            $info['name'] = $this->load('groupActivity')->getGiftName($info['aid']);
                            $info['curTime'] = time();
                            $info['gName'] = $this->redis->hget(REDIS_PREFIX.'GroupInfo_'.$gid,'name');
                            $info['header'] = $this->redis->hget(REDIS_PREFIX.'GroupInfo_'.$gid,'header');
                            $activity[] = $info;
                        }
                    }
                }else{
                    $info['joinNum'] = $this->load('groupActivity')->joinNum($info['gid'],$info['aid']);
                    $info['type'] = intval($info['type']);
                    $info['curTime'] = time();
                    $info['gName'] = '';
                    $info['header'] = '';
                    $activity[] = $info;
                }
            }
        }

        $activity = array_slice($activity,0,$offset);
        $this->result['result']['list'] = $activity;
    }

    /**
     * 全部活动列表
     */
    public function doactivityList(){
        $uid = $this->uid;
        $aid = common::request('aid','G',0);
        $direc = common::request('direc',"G",'next');
        $offset = common::request('offset','G',20);
        $aids =  $this->load('groupActivity')->getActList(0,$aid,$direc,$offset);
        $activity = array();
        foreach($aids as $id){
            $info = $this->load('groupActivity')->getActByAid($id['aid']);
            $info['name'] = '';
            $info['header'] = '';
            if($info['gid'] > 0){
                $gInfo = $this->load('group')->getGroupInfo($info['gid']);
                $info['name'] = $gInfo['name'];
                $info['header'] = $gInfo['header'];
            }
            $info['isJoin'] = $this->load('groupActivity')->isJoin($info['aid'],$uid,$info['gid']);
            $info['joinNum'] = $this->load('groupActivity')->joinNum($info['gid'],$info['aid']);
            $info['cost'] = intval($info['cost']);
            $info['type'] = intval($info['type']);
            $info['groupStatus'] = $this->load('group')->getGroupStatus($uid,$info['gid']);
            $info['curTime'] = time();
            $activity[] = $info;
        }
        
        $banner = $this->load('banner')->getActRecBanner();
        $this->result['result'] = compact('banner','activity');
    }

    /**
     * 群组活动列表
     */
    public function dogroupActivityList(){
        $aid = common::request('aid','G',0);
        $gid = common::request('gid','G');
        $direc = common::request('direc','G','next');
        $offset = common::request('offset','G',20);
        $uid = $this->uid;
        if(empty($gid)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $activity = array();
        $aids = $this->load('groupActivity')->getActList($gid,$aid,$direc,$offset);
        foreach($aids as $id){
            $info = $this->load('groupActivity')->getActByAid($id);
            if(isset($info['aid'])){
                $info['isJoin'] = $this->load('groupActivity')->isJoin($info['aid'],$uid,$gid);
                $info['joinNum'] = $this->load('groupActivity')->joinNum($gid,$info['aid']);
                $info['type'] = intval($info['type']);
                $info['gid'] = $gid;
                $info['name'] = $this->load('groupActivity')->getGiftName($info['aid']);
                $info['curTime'] = time();
                $info['subTitle'] = isset($info['short_desc']) ? $info['short_desc'] : '';
                $info['isNotice'] = intval($this->redis->sismember(REDIS_PREFIX."GAJoin_".$gid."_".$info['aid'],$uid));
                $info['isClaim'] = intval($this->redis->sismember(REDIS_PREFIX."ACTaccept_".$info['aid'],$gid));
                $activity[] = $info;
            }
        }
        $this->result['result']['activity'] = $activity;
    }

    /**
     * 参加活动（没有绑定商品付费活动）
     */
//    public function dojoinActivity(){
//        $aid = common::request('aid','G','',true);
//        $name = common::request('name','G','',true);
//        $phone = common::request('phone','G','',true);
//        $gid = common::request('gid','G','',true);
//        $uid = $this->uid;
//        if(empty($uid)){
//            $_ENV['error']->getErrors(201);
//            exit;
//        }
//        if(empty($aid) || empty($name) || empty($phone)){
//            $_ENV['error']->getErrors(101);
//            exit;
//        }
//        $actInfo = $this->load('groupActivity')->getActByAid($aid);
//        $this->db->insertRowTable('r_collect_info',['aid'=>$aid,'name'=>$name,'phone'=>$phone,'addtime'=>time(),'uid'=>$uid]);
//        $fee = $actInfo['cost'];
//        $this->in_erpDb();
//        $orderInfo = [
//            'aid'          => $aid,
//            'order_id'     => $this->load('order')->createOrderSn(),
//            'goods_id'     => 0,
//            'num'          => 1,
//            'client_type'  => common::getOs(),
//            'uid'          => $uid,
//            'total_fee'    => $fee,
//            'price'        => $fee,
//            'status'       => 2,
//            'consignee_id' => 0,
//            'addtime'      => time(),
//            'updatetime'   => time(),
//            'gid'          => $gid
//        ];
//        $this->erpDb->insertRowTable('r_order',$orderInfo);
//        $this->result['result']['is_success'] = 1;
//    }

    /*
     * 获得活动类型
     */
    public function dogetType(){
        $aid = common::request('aid','G');
        if(empty($aid)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $actInfo = $this->load('groupActivity')->getActByAid($aid);
        if(empty($actInfo)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $uid = $this->uid;
        if(empty($uid)){
            $_ENV['error']->getErrors(201);
            exit;
        }
        $this->result['result']['type'] = intval($actInfo['type']);

    }

    /*
     * 获得上次参赛个人信息
     */
    public function dogetLastCollectInfo(){
        $uid = $this->uid;
        if(empty($uid)){
            $_ENV['error']->getErrors(201);
            exit;
        }
        
        $this->result['result'] = $this->db->getRowTable('r_collect_info',array('name','phone','gender'),'uid='.$uid,'id desc');
    }

    /*
     * 修改证书个人信息
     */
    public function doputCertificate(){
        $id = common::request('id','P');
        $nickname = trim(common::request('nickname','P'));
        
        $header = '';
        $uid = $this->uid;
        if(empty($uid)){
            $_ENV['error']->getErrors(201);
            exit;
        }
        if(empty($id) || empty($nickname)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        if($this->load('badwords')->check($nickname)){
            $_ENV['error']->getErrors(605);
            exit;
        }
        if(isset($_FILES['header']))
        {
            $imgName = md5($uid.common::getHashStr(6).$this->time);

            $newHeaderSource = $this->load('qiniu')->uploadQiNiuImg('header', $imgName);

            if($newHeaderSource)
            {
                $header = $this->load('qiniu')->qiniuPersist($newHeaderSource, '?imageMogr2/thumbnail/200x200');
            }
        }
        $activity_mod = $this->load('groupActivity');
        $activity_mod->putCertificate($id,$uid,$nickname,$header);
        $reData = $activity_mod->getCertificateInfoById($id);
        $reData['actInfo'] = $activity_mod->getActByAid($reData['aid']);
        $this->result['result'] = $reData;
    }
}
